﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Phoenix.Xna.Framework;
using Phoenix.Xna.Framework.Components;
using Phoenix.Xna.Framework.Graphics.Models;

namespace EngineTest.Components.Vegitation
{
    public class Tree : DrawableComponent
    {
        private const string Asset = "Vegitation\\Tree\\Tree";
        private InstancedModel _model;
        private List<Matrix> _instanceTransforms = new List<Matrix>();

        public Tree()
            : base()
        {
        }

        public Tree(string id)
            : base(id)
        {
        }

        public List<Matrix> InstanceTransforms
        {
            get { return _instanceTransforms; }
            set
            {
                _instanceTransforms = value;

                if (_model != null)
                {
                    _model.InstanceTransforms = _instanceTransforms;
                }
            }
        }

        #region DrawableComponent Members

        public override void LoadContent()
        {
            _model = Engine.Instance.Content.Load<InstancedModel>(Asset);
            _model.Initialize();
            _model.InstanceTransforms = _instanceTransforms;
            base.LoadContent();
        }

        public override void UnloadContent()
        {
            Engine.Instance.Content.Release(Asset);
            base.UnloadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (Visible)
            {
                _model.Draw(gameTime);
                base.Draw(gameTime);
            }
        }

        #endregion

        #region Component Members

        public override void Update(GameTime gameTime)
        {
            if (Enabled)
            {
                _model.Update(gameTime);
                base.Update(gameTime);
            }
        }

        #endregion
    }
}
